<template>
	<view class="setUp-root">
		<view class="list">
			<view class="info" @click="upImg" style="padding: 4rpx 20rpx;">
				<view class="txt">头像</view>
				<view class="head-img">
					<image :src="avatar" mode=""></image>
				</view>
				<view class="right">
					<u-icon name="arrow-right" class="arrow"></u-icon>
				</view>
			</view>
			<view class="info" @click="GOnickname" style="border: none;">
				<view class="txt">昵称</view>
				<view class="">{{nickname}}</view>
				<view class="right">
					<u-icon name="arrow-right" class="arrow"></u-icon>
				</view>
			</view>
		</view>
		<u-popup v-model="show" mode="center" height="320" width="710" border-radius="25">
			<view class="popup">
				<view class="success">修改昵称</view>
				<view class="sucorder">
					<input type="text" placeholder="请输入昵称" v-model="nickname"/>
				</view>
				<view class="but allbtn" @click="baocun">保存</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				avatar: '',
				nickname: ''
			}
		},
		onShow() {
			this.userInfo()
		},
		methods: {
			baocun(){
				let data = {
					type:'nickname',
				    val:this.nickname
				}
				this.$Request.post(this.$api.user.editInfo,data).then(res => {
					uni.showToast({
						title: res.msg,
						icon:'none'
					})
					this.show = false;
				});
			},
			userInfo() {
				this.$Request.get(this.$api.user.index).then(res => {
					this.avatar = res.data.userInfo.avatar;
					this.nickname = res.data.userInfo.nickname;
				});
			},
			upImg() {
				let that = this
				uni.chooseImage({
					count:1,
					sourceType: ['album','camera'], //从相册选择
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
				    success: (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
				        uni.uploadFile({
				            url: that.$configs.APIHOST + that.$api.user.file, //仅为示例，非真实的接口地址
				            filePath: tempFilePaths[0],
				            name: 'file',
				            header:{
				                "Authorization": 'Bearer '+uni.getStorageSync('USERINFO')
				            },
				            success: (uploadFileRes) => {
				              
				                that.avatar = JSON.parse(uploadFileRes.data).data.url
								let data = {
									type:'avatar',
								    val:that.avatar
								}
								that.$Request.post(that.$api.user.editInfo,data).then(res => {
									uni.showToast({
										title: res.msg,
										icon:'none'
									})
								});
				            }
				        });
				    }
				});
			},
			GOnickname() {
				this.show = true;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup {
		.success{
			display: flex;
			justify-content: center;
			align-items: center;
			font-weight: bold;
			margin-top: 40rpx;font-size: 32rpx;color: #333;
		}
		.sucorder{
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			color: #666666;
			margin-top: 20rpx;
			margin-bottom: 30rpx;
		}
		.title{
			padding-top: 40rpx;
			width: 100%;
			font-size: 40rpx;
			color: #FF6505;
			display: flex;
			justify-content: center;
			align-items: center;
			text:nth-of-type(1){
				font-size: 30rpx;
				color: #333333;
			}
			text:nth-of-type(2){
				font-size: 48rpx;
				color: #FF1403;
			}
			text:nth-of-type(3){
				font-size: 30rpx;
				color: #333333;
			}
			text:nth-of-type(4){
				font-size: 48rpx;
				color: #FF1403;
			}
		}
		.sec{
			width: 100%;
			font-size: 32rpx;
			color: #333;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 60rpx;
			font-weight: bold;
			padding-top: 18rpx;
		}
		.but {
			margin: 0 auto;
			width: 660rpx;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 10rpx;
			text-align: center;
			color: #000;
			font-size: 30rpx;
		}
	}
	.setUp-root {
		width: 100%;
		min-height: 100vh;
		background: #f8f8f8;
		position: relative;
		padding-top: 10rpx;
		box-sizing: border-box;

		.list {
			background-color: #FFFFFF;

			.info {
				height: 112rpx;
				display: flex;
				padding: 25rpx 20rpx;
				align-items: center;
				border-bottom: solid 1upx #eee;
				.txt {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #282828;
					line-height: 40rpx;
					flex: 1;
				}

				.head-img {
					width: 82rpx;
					height: 82rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 41rpx;
					}
				}

				.img {
					width: 30rpx;
					height: 30rpx;
					margin-left: 15rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.right {
					display: flex;
					align-items: center;
					color: #000;
						margin-left: 15rpx;
					.tis {
						font-size: 26upx;
						margin-right: 5upx;
						max-height: 120upx;

						image {
							width: 120upx;
							height: 120upx;
							margin: 10upx 0;
						}
					}

					.icon {
						width: 40upx;
						color: #cecece;
					}
				}
			}
		}
	}
</style>
